/*****************************************************************************
*Purpose: Estimate core results for Children of Gautreaux Participants 
******************************************************************************/ 


global fe "intakeyear yrbirth  mitrct80"
global notractfe "intakeyear yrbirth  "
global controls "female head_female headage married auto license work emp inc chicago ph i.bed"


********************************
*********** LEHD Main **********
********************************
global outlist "earn24 earn26 earn30 earn33 earn38 avgearn2628 avgearn2428 avgearn2933 avgearn3438 avgemply2428 avgemply2933 avgemply3438 cumul_earn28 cumul_earn33 cumul_earn38 "


capture frame drop lehd
frame create lehd str35 outcome str35 group str35 spec beta se obs piks treatpik controlpik r2

foreach gr in all girl boy  lt13 {
foreach spec in base notractfe nocont meansd { 
foreach out in $outlist {


	**** Base Specification
	if "`spec'"=="base" {
	reghdfe `out' generalarea $controls if  estsample==1 & `gr'==1, absorb( $fe ) cluster(hhid)
	gen samp_`out'_`gr'=e(sample)
		
	unique pik if e(sample)==1
	local allpik `=r(sum)'
	unique pik if e(sample)==1 & generalarea==1
	local treatpik `=r(sum)'
	unique pik if e(sample)==1 & generalarea==0
	local controlpik `=r(sum)'
	frame post lehd ("`out'") ("`gr'") ("`spec'") (_b[generalarea]) (_se[generalarea]) (e(N)) (`allpik') (`treatpik') (`controlpik') (e(r2))
	}
	
	**** No Controls, with FE Specification
	if "`spec'"=="nocont" {
	reghdfe `out' generalarea  if  samp_`out'_`gr'==1 & `gr'==1, absorb( $fe ) cluster(hhid)
	unique pik if e(sample)==1
	local allpik `=r(sum)'
	unique pik if e(sample)==1 & generalarea==1
	local treatpik `=r(sum)'
	unique pik if e(sample)==1 & generalarea==0
	local controlpik `=r(sum)'
	frame post lehd ("`out'") ("`gr'") ("`spec'") (_b[generalarea]) (_se[generalarea]) (e(N)) (`allpik') (`treatpik') (`controlpik') (e(r2))
			
	}
	
	**** No Tract-FE, with Controls
	if "`spec'"=="notractfe" {
	reghdfe `out' generalarea $controls if  samp_`out'_`gr'==1 & `gr'==1, absorb( $notractfe ) cluster(hhid)
	unique pik if e(sample)==1
	local allpik `=r(sum)'
	unique pik if e(sample)==1 & generalarea==1
	local treatpik `=r(sum)'
	unique pik if e(sample)==1 & generalarea==0
	local controlpik `=r(sum)'
	frame post lehd ("`out'") ("`gr'") ("`spec'") (_b[generalarea]) (_se[generalarea]) (e(N)) (`allpik') (`treatpik') (`controlpik') (e(r2))
			
	}
	
	**** Control Mean
	if "`spec'"=="meansd" {
	sum `out' if generalarea!=1 & samp_`out'_`gr'==1 & `gr'==1
	local controlmean `=r(mean)'
	local controlsd `=r(sd)'
	unique pik if generalarea!=1 & samp_`out'_`gr'==1 
	frame post lehd ("`out'") ("`gr'") ("`spec'") (`controlmean') (`controlsd') (r(sum)) (r(sum)) (r(sum)) (r(sum)) (r(sum))
			
	}

	
	}
}
}


frame lehd: save "${output}/kids_lehd.dta", replace
frame lehd: export delimited using "${output}/kids_lehd.csv", replace



********************************
********** HHFE Robust *********
********************************
global hhfe_fe "hhid age26year chicago female"
global clusx "hhid"



global ageint "ageXcar ageXmarried ageXwork ageXlicense ageXph genderXage femhohXage "

*** Globals
global hhfe_cont "hhid yrbirth chicago female"
global hhfe_bin "hhid yrbirth chicago female"
global clusx "hhid"
	

capture frame drop hhfe
frame create hhfe str35 outcome str35 group str35 spec str35 treatvar beta se obs piks treatpik controlpik

foreach hout in earn26 avgearn2628 avgearn2428 avgearn2933 cumul_earn28 {

	** HHFE - Continous Exposure / Binary Treatment:  
	reghdfe `hout' ageXtreat if estsample==1 , absorb($hhfe_cont) cluster($clusx)
	unique pik if e(sample)==1
	local allpik `=r(sum)'
	unique pik if e(sample)==1  
	local y1treatpik `=r(sum)'
	unique pik if e(sample)==1  
	local y1controlpik `=r(sum)'
	
	frame post hhfe ("`hout'") ("all") ("hhfe_continuous") ("ageXtreat") (_b[ageXtreat]) (_se[ageXtreat]) (e(N)) (`allpik') (-99999) (-99999) 

	** HHFE - Continous Exposure / Binary Treatment: Age interaction controls
	reghdfe `hout' ageXtreat $ageint if estsample==1 , absorb($hhfe_cont) cluster($clusx)
	unique pik if e(sample)==1
	local allpik `=r(sum)'
	unique pik if e(sample)==1  
	local y1treatpik `=r(sum)'
	unique pik if e(sample)==1  
	local y1controlpik `=r(sum)'
	
	frame post hhfe ("`hout'") ("all") ("hhfe_continuous_ageint") ("ageXtreat") (_b[ageXtreat]) (_se[ageXtreat]) (e(N)) (`allpik') (-99999) (-99999) 

	** HHFE - Continous Exposure / Binary Treatment: Car X Age controls
	reghdfe `hout' ageXtreat ageXcar if estsample==1 , absorb($hhfe_cont) cluster($clusx)
	unique pik if e(sample)==1
	local allpik `=r(sum)'
	unique pik if e(sample)==1  
	local y1treatpik `=r(sum)'
	unique pik if e(sample)==1  
	local y1controlpik `=r(sum)'
	
	frame post hhfe ("`hout'") ("all") ("hhfe_continuous_agecar") ("ageXtreat") (_b[ageXtreat]) (_se[ageXtreat]) (e(N)) (`allpik') (-99999) (-99999) 
	

	** HHFE - Binary Age Exposure
	reghdfe `hout' youngerTreat younger if estsample==1 , absorb( $hhfe_bin) cluster($clusx)
	unique pik if e(sample)==1
	local allpik `=r(sum)'
	unique pik if e(sample)==1 & youngerTreat==1  
	local y1treatpik `=r(sum)'
	unique pik if e(sample)==1 & youngerTreat==0  
	local y1controlpik `=r(sum)'
	
	
	frame post hhfe ("`hout'") ("all") ("hhfe_binary") ("youngerTreat") (_b[youngerTreat]) (_se[youngerTreat]) (e(N)) (`allpik') (`y1treatpik') (`y1controlpik') 
	
	*** HHFE Sample / No HHFE - Binary Treatment
	reghdfe `hout' youngerTreat younger if e(sample)==1 , absorb($fe) cluster($clusx)
	unique pik if e(sample)==1
	local allpik `=r(sum)'
	unique pik if e(sample)==1 & youngerTreat==1  
	local y1treatpik `=r(sum)'
	unique pik if e(sample)==1 & youngerTreat==0  
	local y1controlpik `=r(sum)'
	
	
	frame post hhfe ("`hout'") ("all") ("hhfesamp_nohhfe") ("youngerTreat") (_b[youngerTreat]) (_se[youngerTreat]) (e(N)) (`allpik') (`y1treatpik') (`y1controlpik') 
	
	*** Base Spec in HHFE Sample
	reghdfe `hout' generalarea $controls if e(sample)==1 , absorb($fe) cluster($clusx)
	unique pik if e(sample)==1
	local allpik `=r(sum)'
	unique pik if e(sample)==1 & generalarea==1  
	local y1treatpik `=r(sum)'
	unique pik if e(sample)==1 & generalarea==0  
	local y1controlpik `=r(sum)'
	
	
	frame post hhfe ("`hout'") ("all") ("basespec_hhfe") ("generalarea") (_b[generalarea]) (_se[generalarea]) (e(N)) (`allpik') (`y1treatpik') (`y1controlpik') 
	

	*** HHFE Sample / No Controls - Binary Treatment / Binary Age
	reghdfe `hout' youngerTreat younger if estsample==1  , absorb( $hhfe_bin) cluster($clusx)
	unique pik if e(sample)==1
	local allpik `=r(sum)'
	unique pik if e(sample)==1 & youngerTreat==1  
	local y1treatpik `=r(sum)'
	unique pik if e(sample)==1 & youngerTreat==0  
	local y1controlpik `=r(sum)'
	
	
	frame post hhfe ("`hout'") ("all") ("hhfe_binary_nocont") ("youngerTreat") (_b[youngerTreat]) (_se[youngerTreat]) (e(N)) (`allpik') (`y1treatpik') (`y1controlpik') 

	


}


frame hhfe: save "${output}/lehd_hhfe.dta", replace
frame hhfe: export delimited using "${output}/lehd_hhfe.csv", replace


********************************
********* Robustness *********** 
********************************

capture frame drop earnrobust
frame create earnrobust str35 outcome str35 group str35 spec beta se obs piks treatpiks controlpiks 

foreach rout in earn24 earn26 avgearn2428  {
** Base
reghdfe `rout' generalarea $controls if  estsample==1 , absorb( $fe ) cluster(hhid)
	unique pik if e(sample)==1
	local allpik `=r(sum)'
	unique pik if e(sample)==1 & generalarea==1
	local treatpik `=r(sum)'
	unique pik if e(sample)==1 & generalarea==0
	local controlpik `=r(sum)'
	
frame post earnrobust  ("`rout'") ("all") ("base") (_b[generalarea]) (_se[generalarea]) (e(N)) (`allpik') (`treatpik') (`controlpik')

	
** Low Vac Years
reghdfe `rout' generalarea $controls if lowvac==1 & estsample==1 , absorb( $fe ) cluster(hhid)
	unique pik if e(sample)==1
	local allpik `=r(sum)'
	unique pik if e(sample)==1 & generalarea==1
	local treatpik `=r(sum)'
	unique pik if e(sample)==1 & generalarea==0
	local controlpik `=r(sum)'

frame post earnrobust  ("`rout'") ("all") ("lowvac") (_b[generalarea]) (_se[generalarea]) (e(N)) (`allpik') (`treatpik') (`controlpik')


** 1st Offers only
reghdfe `rout' generalarea $controls if offers==1 & estsample==1 , absorb( $fe ) cluster(hhid)
	unique pik if e(sample)==1
	local allpik `=r(sum)'
	unique pik if e(sample)==1 & generalarea==1
	local treatpik `=r(sum)'
	unique pik if e(sample)==1 & generalarea==0
	local controlpik `=r(sum)'

frame post earnrobust  ("`rout'") ("all") ("1stoffer") (_b[generalarea]) (_se[generalarea]) (e(N)) (`allpik') (`treatpik') (`controlpik')


** 1st Offers only & low-vac
reghdfe `rout' generalarea $controls if lowvac==1 & offers==1 & estsample==1 , absorb( $fe ) cluster(hhid)
	unique pik if e(sample)==1
	local allpik `=r(sum)'
	unique pik if e(sample)==1 & generalarea==1
	local treatpik `=r(sum)'
	unique pik if e(sample)==1 & generalarea==0
	local controlpik `=r(sum)'

frame post earnrobust  ("`rout'") ("all") ("1stofflowvac") (_b[generalarea]) (_se[generalarea]) (e(N)) (`allpik') (`treatpik') (`controlpik')


** Exclude <90
reghdfe `rout' generalarea $controls if intakeyear<1990 & estsample==1 , absorb( $fe ) cluster(hhid)
	unique pik if e(sample)==1
	local allpik `=r(sum)'
	unique pik if e(sample)==1 & generalarea==1
	local treatpik `=r(sum)'
	unique pik if e(sample)==1 & generalarea==0
	local controlpik `=r(sum)'

frame post earnrobust  ("`rout'") ("all") ("exclude90") (_b[generalarea]) (_se[generalarea]) (e(N)) (`allpik') (`treatpik') (`controlpik')

** Include pre-82
reghdfe `rout' generalarea $controls if diff==1 , absorb( $fe ) cluster(hhid)
	unique pik if e(sample)==1
	local allpik `=r(sum)'
	unique pik if e(sample)==1 & generalarea==1
	local treatpik `=r(sum)'
	unique pik if e(sample)==1 & generalarea==0
	local controlpik `=r(sum)'

frame post earnrobust  ("`rout'") ("all") ("include70") (_b[generalarea]) (_se[generalarea]) (e(N)) (`allpik') (`treatpik') (`controlpik')


** Suburb
reghdfe `rout' suburb $controls if estsample==1 , absorb( $fe ) cluster(hhid)
	unique pik if e(sample)==1
	local allpik `=r(sum)'
	unique pik if e(sample)==1 & suburb==1
	local treatpik `=r(sum)'
	unique pik if e(sample)==1 & suburb==0
	local controlpik `=r(sum)'

frame post earnrobust  ("`rout'") ("all") ("suburb") (_b[suburb]) (_se[suburb]) (e(N)) (`allpik') (`treatpik') (`controlpik')


** Majority White
reghdfe `rout' place_majoritywhite $controls if estsample==1 , absorb( $fe ) cluster(hhid)
	unique pik if e(sample)==1
	local allpik `=r(sum)'
	unique pik if e(sample)==1 & place_majoritywhite==1
	local treatpik `=r(sum)'
	unique pik if e(sample)==1 & place_majoritywhite==0
	local controlpik `=r(sum)'

frame post earnrobust  ("`rout'") ("all") ("majoritywhite") (_b[place_majoritywhite]) (_se[place_majoritywhite]) (e(N)) (`allpik') (`treatpik') (`controlpik')


** Suburb
reghdfe `rout' suburb $controls if estsample==1 , absorb( $fe ) cluster(hhid)
	unique pik if e(sample)==1
	local allpik `=r(sum)'
	unique pik if e(sample)==1 & suburb==1
	local treatpik `=r(sum)'
	unique pik if e(sample)==1 & suburb==0
	local controlpik `=r(sum)'

frame post earnrobust  ("`rout'") ("all") ("suburb") (_b[suburb]) (_se[suburb]) (e(N)) (`allpik') (`treatpik') (`controlpik')


** Continuous measure: black
reghdfe `rout' std_place_povrate $controls if estsample==1 , absorb( $fe ) cluster(hhid)
	unique pik if e(sample)==1
	local allpik `=r(sum)'
	unique pik if e(sample)==1  
	local treatpik `=r(sum)'
	unique pik if e(sample)==1  
	local controlpik `=r(sum)'

frame post earnrobust  ("`rout'") ("all") ("stdpovrate") (_b[std_place_povrate]) (_se[std_place_povrate]) (e(N)) (`allpik') (`treatpik') (`controlpik')



** Continuous measure of race
reghdfe `rout' std_place_shwhite $controls if estsample==1 , absorb( $fe ) cluster(hhid)
	unique pik if e(sample)==1
	local allpik `=r(sum)'
	unique pik if e(sample)==1  
	local treatpik `=r(sum)'
	unique pik if e(sample)==1  
	local controlpik `=r(sum)'

frame post earnrobust  ("`rout'") ("all") ("stdshwhite") (_b[std_place_shwhite]) (_se[std_place_shwhite]) (e(N)) (`allpik') (`treatpik') (`controlpik')


** Continuous measure: black
reghdfe `rout' std_place_shafam $controls if estsample==1 , absorb( $fe ) cluster(hhid)
	unique pik if e(sample)==1
	local allpik `=r(sum)'
	unique pik if e(sample)==1  
	local treatpik `=r(sum)'
	unique pik if e(sample)==1  
	local controlpik `=r(sum)'

frame post earnrobust  ("`rout'") ("all") ("stdshafam") (_b[std_place_shafam]) (_se[std_place_shafam]) (e(N)) (`allpik') (`treatpik') (`controlpik')



** HR: white v. pov
reghdfe `rout' std_place_shwhite std_place_povrate $controls if estsample==1 , absorb( $fe ) cluster(hhid)
	unique pik if e(sample)==1
	local allpik `=r(sum)'
	unique pik if e(sample)==1  
	local treatpik `=r(sum)'
	unique pik if e(sample)==1  
	local controlpik `=r(sum)'

frame post earnrobust  ("`rout'") ("all") ("HRwhite_v_pov_betapov") (_b[std_place_povrate]) (_se[std_place_povrate]) (e(N)) (`allpik') (`treatpik') (`controlpik')

frame post earnrobust  ("`rout'") ("all") ("HRwhite_v_pov_betawhite") (_b[std_place_shwhite ]) (_se[std_place_shwhite ]) (e(N)) (`allpik') (`treatpik') (`controlpik')

** HR: black v. pov
reghdfe `rout' std_place_shafam std_place_povrate $controls if estsample==1 , absorb( $fe ) cluster(hhid)
	unique pik if e(sample)==1
	local allpik `=r(sum)'
	unique pik if e(sample)==1  
	local treatpik `=r(sum)'
	unique pik if e(sample)==1  
	local controlpik `=r(sum)'

frame post earnrobust  ("`rout'") ("all") ("HRblack_v_pov_betapov") (_b[std_place_povrate]) (_se[std_place_povrate]) (e(N)) (`allpik') (`treatpik') (`controlpik')

frame post earnrobust  ("`rout'") ("all") ("HRblack_v_pov_betablack") (_b[std_place_shafam]) (_se[std_place_shafam ]) (e(N)) (`allpik') (`treatpik') (`controlpik')


}

frame earnrobust: save "${output}/lehd_robustness.dta", replace
frame earnrobust: export delimited using "${output}/lehd_robustness.csv", replace





********************************


capture frame drop earnHR
frame create earnHR str35 outcome str35 indepvar str35 spec beta se obs piks str65 concat 

*std_place_povrate std_place_shwhite std_place_shafam std_place_persing std_place_wba  std_place_medinc std_place_incrank std_place_black_incrank std_place_jail std_place_black_jail


foreach x in shwhite shafam povrate  {

foreach rout in earn24 avgearn2428 avgearn2628 avgearn2933 {

reghdfe `rout' std_place_shwhite std_place_povrate $controls if  estsample==1 , absorb( $fe ) cluster(hhid)
replace consistoutsamp=e(sample)

foreach z in shwhite shafam povrate incrank exp_ec_zip  { 
** Bivariate
reghdfe `rout' std_place_`z'  $controls miss_`z' x_miss_`z' if  estsample==1 & consistoutsamp==1, absorb( $fe ) cluster(hhid)
	unique pik if e(sample)==1
	local allpik `=r(sum)'
	unique pik if e(sample)==1 & generalarea==1
	local treatpik `=r(sum)'
	unique pik if e(sample)==1 & generalarea==0
	local controlpik `=r(sum)'
	
frame post earnHR  ("`rout'") ("`z'") ("`z'")  (_b[std_place_`z']) (_se[std_place_`z']) (e(N)) (`allpik') ("`rout'_`z'_`z'")
}

** Multivariate: x v. Poverty
reghdfe `rout' std_place_`x' std_place_povrate $controls miss_`x' x_miss_`x' if  estsample==1 & consistoutsamp==1 , absorb( $fe ) cluster(hhid)
	unique pik if e(sample)==1
	local allpik `=r(sum)'
	unique pik if e(sample)==1 & generalarea==1
	local treatpik `=r(sum)'
	unique pik if e(sample)==1 & generalarea==0
	local controlpik `=r(sum)'
	
frame post earnHR  ("`rout'") ("`x'") ("povrate_v_`x'") (_b[std_place_`x']) (_se[std_place_`x']) (e(N)) (`allpik')  ("`rout'_`x'_povrate_v_`x'")
frame post earnHR  ("`rout'") ("povrate") ("povrate_v_`x'") (_b[std_place_povrate]) (_se[std_place_povrate]) (e(N)) (`allpik') ("`rout'_povrate_povrate_v_`x'")  


** Multivariate: x v. Opp Atlas
reghdfe `rout' std_place_`x' std_place_incrank $controls miss_`x' x_miss_`x' if  estsample==1 & consistoutsamp==1 , absorb( $fe ) cluster(hhid)
	unique pik if e(sample)==1
	local allpik `=r(sum)'
	unique pik if e(sample)==1 & generalarea==1
	local treatpik `=r(sum)'
	unique pik if e(sample)==1 & generalarea==0
	local controlpik `=r(sum)'
	
frame post earnHR  ("`rout'") ("`x'") ("incrank_v_`x'") (_b[std_place_`x']) (_se[std_place_`x']) (e(N)) (`allpik')  ("`rout'_`x'_incrank_v_`x'")
frame post earnHR  ("`rout'") ("incrank") ("incrank_v_`x'") (_b[std_place_incrank]) (_se[std_place_incrank]) (e(N)) (`allpik') ("`rout'_incrank_incrank_v_`x'")  

}

}


frame earnHR: drop if spec=="povrate_v_povrate"
frame earnHR: save "${output}/earnHR_kids.dta", replace
frame earnHR: export delimited using "${output}/earnHR_kids.csv", replace



********************************
***** Earnings School Qual *****
********************************




capture frame drop earnSchool
frame create earnSchool str35 outcome str35 indepvar str35 spec beta se obs piks treatpik controlpik str65 concat


foreach zout in avgearn2428  {
foreach v of varlist sch_exp_perstd sch_pupil_pertch sch_black_share sch_white_share {


** Binary
reghdfe `zout' generalarea `v' $controls miss_`v' x_miss_`v' if  estsample==1 , absorb( $fe ) cluster(hhid)
	unique pik if e(sample)==1
	local allpik `=r(sum)'
	unique pik if e(sample)==1 & generalarea==1
	local treatpik `=r(sum)'
	unique pik if e(sample)==1 & generalarea==0
	local controlpik `=r(sum)'
	
frame post earnSchool  ("`zout'") ("generalarea") ("generalarea_v_`v'") (_b[generalarea]) (_se[generalarea]) (e(N)) (`allpik') (`treatpik') (`controlpik') ("generalarea_generalarea_v_`v'")
frame post earnSchool  ("`zout'") ("`v'") ("generalarea_v_`v'") (_b[`v']) (_se[`v']) (e(N)) (`allpik') (`treatpik') (`controlpik') ("`v'_generalarea_v_`v'")

** Continuous
reghdfe `zout' std_place_shwhite `v' $controls miss_`v' x_miss_`v' if  estsample==1 , absorb( $fe ) cluster(hhid)
	unique pik if e(sample)==1
	local allpik `=r(sum)'
	unique pik if e(sample)==1 & generalarea==1
	local treatpik `=r(sum)'
	unique pik if e(sample)==1 & generalarea==0
	local controlpik `=r(sum)'
	
frame post earnSchool  ("`zout'") ("shwhite") ("shwhite_v_`v'") (_b[std_place_shwhite]) (_se[std_place_shwhite]) (e(N)) (`allpik') (.) (.) ("shwhite_shwhite_v_`v'")
frame post earnSchool  ("`zout'") ("`v'") ("shwhite_v_`v'") (_b[`v']) (_se[`v']) (e(N)) (`allpik') (.) (.) ("`v'_shwhite_v_`v'")

}


*** All school controls : binary treatment
reghdfe `zout' generalarea sch_exp_perstd sch_pupil_pertch sch_white_share miss_sch_white_share x_miss_sch_white_share miss_sch_pupil_pertch x_miss_sch_pupil_pertch x_miss_sch_exp_perstd miss_sch_exp_perstd $controls if  estsample==1 , absorb( $fe ) cluster(hhid)
	unique pik if e(sample)==1
	local allpik `=r(sum)'
	unique pik if e(sample)==1 & generalarea==1
	local treatpik `=r(sum)'
	unique pik if e(sample)==1 & generalarea==0
	local controlpik `=r(sum)'
 
frame post earnSchool  ("`zout'") ("generalarea") ("generalarea_v_allschool") (_b[generalarea]) (_se[generalarea]) (e(N)) (`allpik') (`treatpik') (`controlpik') ("generalarea_generalarea_v_allschool")

*** All school controls : continuous treatment
reghdfe `zout' std_place_shwhite sch_exp_perstd sch_pupil_pertch sch_white_share miss_sch_white_share x_miss_sch_white_share miss_sch_pupil_pertch x_miss_sch_pupil_pertch x_miss_sch_exp_perstd miss_sch_exp_perstd $controls if  estsample==1 , absorb( $fe ) cluster(hhid)
 
frame post earnSchool  ("`zout'") ("shwhite") ("shwhite_v_allschool") (_b[std_place_shwhite]) (_se[std_place_shwhite]) (e(N)) (`allpik')  (.) (.) ("shwhite_shwhite_v_allschool")


}

frame change earnSchool
compress *
*duplicates drop * ,force
frame change default

frame earnSchool: save "${output}/lehd_schools_results.dta", replace
frame earnSchool: export delimited using "${output}/lehd_schools_results.csv", replace
